#include "stdafx.h"
#include "cControl.h"
#include "cNotesFolderInfo.h"
#include "cUser.h"
#include "CUserFromFile.h"
#include "SortableObjArray.h"
#include "cQuickPlaceDB.h"

IMPLEMENT_DYNAMIC( cControl, CObject)

cControl::cControl()
{
	
	pLog=NULL;
	pResolver=NULL;
	lpMapAddress=NULL;
	hMapFile=NULL;
	fIsNotesInitialized=FALSE;
	m_bMergeFolders = FALSE;
	m_strDocumentLibraryName = _T("");
	m_bSubRoomSecurity=FALSE;
	pdb=NULL;
	TIME_ZONE_INFORMATION tz;
	pDebugLog=new cLogger("debug.log");
	pFileLog=new cLogger("Files.log");
	m_bUseDescriptionForBody=FALSE;
	m_bUseFolderForMoreThanTwoAttachments=FALSE;
	m_bAdvanced=TRUE;
	NrOfDocs=0;
	NrOfListItems=0;
  ZeroMemory(&tz,sizeof(tz));
  switch (GetTimeZoneInformation(&tz)) 
		{
			case TIME_ZONE_ID_UNKNOWN:
			case TIME_ZONE_ID_STANDARD:
				m_nBias= (tz.Bias+tz.StandardBias);
					break;
			case TIME_ZONE_ID_DAYLIGHT:
					m_nBias= (tz.Bias+tz.StandardBias+tz.DaylightBias);
					break;

	  } 
	m_bThrowNotesErrors=TRUE;
	m_nLocale=1033; //Default US English
	





}

cControl::~cControl()
{
	try
		{
			
			//Start cleanup
			if(fIsNotesInitialized)
				{
					if(NABdb.IsOpen())
						NABdb.Close();

					session.Term(TRUE);
				}

		
			if(lpMapAddress)
				UnmapViewOfFile(lpMapAddress); 
 								// Close the process's handle to the file-mapping object.
			if(hMapFile)
				CloseHandle(hMapFile); 
 			DeleteFile( m_strTempNotesIni);
			for(int iCustomField=0;iCustomField<=m_objArrayCustomFields.GetUpperBound();iCustomField++)
				{
					cNotesFieldInfo *pCustomField=(cNotesFieldInfo *)m_objArrayCustomFields.GetAt(iCustomField);
					if(pCustomField)
						delete pCustomField;
				}

			for(int iUser=0;iUser<=m_objUsersFromFile.GetUpperBound();iUser++)
				{
					CUserFromFile *pUserFromFile=(CUserFromFile *)m_objUsersFromFile.GetAt(iUser);
					if(pUserFromFile)
						delete pUserFromFile;
				}
			for(int i=0;i<=m_objQuickPlaces.GetUpperBound();i++)
				{
					cQuickPlaceDB *pdb=(cQuickPlaceDB *)m_objQuickPlaces.GetAt(i);
					if(pdb)
						delete pdb;
				}


		if(pLog)
			delete pLog;
		if(pDebugLog)
			delete pDebugLog;
		if(pFileLog)
			delete pFileLog;
		if(pResolver)
			delete pResolver;

		::SysFreeString(bstrSiteTemplate);
		::SysFreeString(bstrContainer);
		::SysFreeString(bstrSiteContainerRoot);
		}
	catch(CException *cEx)
		{
			cEx->Delete();

		}

}
